import { CSS3DSprite } from "three/examples/jsm/renderers/CSS3DRenderer.js";
import * as THREE from "three";
class LabelManager {
  constructor(scene) {
    this.scene = scene;
    this.sprites = [];
  }

  createLabel(content, position, onClick) {
    const spriteDiv = document.getElementById("info");
    // spriteDiv.className = 'sprite';
    // spriteDiv.textContent = content;

    // 添加点击事件处理
    if (onClick) {
      spriteDiv.addEventListener('click', onClick);
    }

    const sprite = new CSS3DSprite(spriteDiv);
    sprite.scale.set(0.025, 0.025, 0.025);
    sprite.position.copy(position);

  
    this.scene.add(sprite);
    this.sprites.push(sprite);

    return sprite;
  }
  showLabel() {
    var element = document.getElementById("info");

    element.setAttribute("class", "info");
  }

  updateLabels() {
    this.sprites.forEach((sprite) => {
      // 这里可以根据需要更新每个标签的位置
      const target = sprite.userData.target; // 假设每个标签存储了其目标对象
      if (target) {
        const worldPosition = new THREE.Vector3();
        target.updateWorldMatrix(true, false);
        target.getWorldPosition(worldPosition);
        sprite.position.copy(worldPosition);
        sprite.position.y += 1; // 可选：调整高度
      }
    });
  }
}

export default LabelManager;
